<?php

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\event\RouteLoaded;

class CmdRoute extends Command
{

    protected function configure()
    {
        // 指令配置
        $this->setName('cmd:route')
            ->setDescription('生成路由12');
    }

    /**
     * @param Input  $input
     * @param Output $output
     * @return bool|void
     */
    protected function execute(Input $input, Output $output)
    {
        $this->app->route->clear();
        $this->app->route->lazy(false);

        // 路由检测
        $path = $this->app->getRootPath() . ($dir ? 'app' . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR : '') . 'route' . DIRECTORY_SEPARATOR;

        $files = is_dir($path) ? scandir($path) : [];

        foreach ($files as $file) {
            if (strpos($file, '.php')) {
                include $path . $file;
            }
        }

        //触发路由载入完成事件
        $this->app->event->trigger(RouteLoaded::class);
        $rules = $this->app->route->getName();

        return '<?php ' . PHP_EOL . 'return unserialize(\'' . serialize($rules) . '\');';
    }
}